package com.xiaoyu.lanling.feature.vip.fragment

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.view.list.GridSpacingItemDecoration
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.vip.model.PrivilegeItem
import com.xiaoyu.lanling.feature.vip.model.ProductItem
import com.xiaoyu.lanling.feature.vip.model.VipProduct
import com.xiaoyu.lanling.feature.vip.viewholder.PrivilegeViewHolder
import com.xiaoyu.lanling.feature.vip.viewholder.ProductViewHolder
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.vip_product_fragment.*

class VipProductFragment : OnShowFragment() {

    companion object {
        fun newInstance(vipProduct: VipProduct): VipProductFragment {
            val params = Bundle()
            params.putSerializable(Constant.IntentKey.KEY_VIP_PRODUCT, vipProduct)

            val fragment = VipProductFragment()
            fragment.arguments = params
            return fragment
        }
    }

    private val privilegeAdapter = CubeRecyclerViewSimpleAdapter<PrivilegeItem>()
    private val productAdapter = CubeRecyclerViewSimpleAdapter<ProductItem>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.vip_product_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initAdapter()
        initData()
    }

    private fun initAdapter() {
        privilegeAdapter.setViewHolderClass(0, null, PrivilegeViewHolder::class.java, 9)
        privilege_recyclerview.adapter = privilegeAdapter
        privilege_recyclerview.addItemDecoration(GridSpacingItemDecoration(3, 48, 16, false))

        productAdapter.setViewHolderClass(0, null, ProductViewHolder::class.java, 3)
        product_recyclerview.adapter = productAdapter
        product_recyclerview.addItemDecoration(GridSpacingItemDecoration(3, 8, 0, false))
    }

    private fun initData() {
        val vipProduct = (arguments?.getSerializable(Constant.IntentKey.KEY_VIP_PRODUCT) as? VipProduct)
                ?: return
        val privilegeSize = vipProduct.privileges.size
        val privilegeDesc = "$privilegeSize/$privilegeSize"

        ImageViewUtil.loadImage(vip_icon, ImageLoadParam.newBuilder().setUrl(vipProduct.vipInfo.icon).setSizeInDP(132).build())
        privilege_overview.text = getString(R.string.vip_product_privilege_overview_title, vipProduct.vipInfo.name, privilegeDesc)

        privilegeAdapter.list = vipProduct.privileges
        privilegeAdapter.notifyDataSetChanged()

        productAdapter.list = vipProduct.products
        productAdapter.notifyDataSetChanged()
    }
}